// 模拟用户数据
const mockUsers = [
  {
    id: 1,
    username: 'admin',
    email: 'admin@example.com',
    password: '123456', // 实际项目中应该是加密后的密码
    token: 'mock-admin-token',
    avatar: '',
    role: 'admin'
  },
  {
    id: 2,
    username: 'user',
    email: 'user@example.com',
    password: '123456',
    token: 'mock-user-token',
    avatar: '',
    role: 'user'
  }
]

// 模拟API延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// 模拟登录API
export const mockLogin = async (credentials: { username: string; password: string }) => {
  await delay(1000) // 模拟网络延迟
  
  const user = mockUsers.find(u => 
    (u.username === credentials.username || u.email === credentials.username) && 
    u.password === credentials.password
  )
  
  if (user) {
    return {
      success: true,
      data: {
        token: user.token,
        user: {
          id: user.id,
          username: user.username,
          email: user.email,
          avatar: user.avatar,
          role: user.role
        }
      }
    }
  } else {
    return {
      success: false,
      message: '用户名或密码错误'
    }
  }
}

// 模拟注册API
export const mockRegister = async (userData: { username: string; email: string; password: string }) => {
  await delay(1000) // 模拟网络延迟
  
  // 检查用户名是否已存在
  const existingUser = mockUsers.find(u => 
    u.username === userData.username || u.email === userData.email
  )
  
  if (existingUser) {
    return {
      success: false,
      message: existingUser.username === userData.username ? '用户名已存在' : '邮箱已被注册'
    }
  }
  
  // 创建新用户
  const newUser = {
    id: mockUsers.length + 1,
    username: userData.username,
    email: userData.email,
    password: userData.password,
    token: `mock-token-${Date.now()}`,
    avatar: '',
    role: 'user'
  }
  
  mockUsers.push(newUser)
  
  return {
    success: true,
    data: {
      token: newUser.token,
      user: {
        id: newUser.id,
        username: newUser.username,
        email: newUser.email,
        avatar: newUser.avatar,
        role: newUser.role
      }
    }
  }
}

// 模拟获取用户信息API
export const mockGetUserInfo = async (token: string) => {
  await delay(500) // 模拟网络延迟
  
  const user = mockUsers.find(u => u.token === token)
  
  if (user) {
    return {
      success: true,
      data: {
        id: user.id,
        username: user.username,
        email: user.email,
        avatar: user.avatar,
        role: user.role
      }
    }
  } else {
    return {
      success: false,
      message: '无效的token'
    }
  }
}

// 模拟退出登录API
export const mockLogout = async () => {
  await delay(500) // 模拟网络延迟
  
  return {
    success: true,
    data: null
  }
}